package com.tlgen.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class GenCodeController {

    @PostMapping("/generate")
    public Object generate(@RequestParam("str") String str) {

        Map<String, Object> ret = new HashMap<>();
        Map<String, Object> result1 = new HashMap<>();
        Map<String, Object> result2 = new HashMap<>();
        Map<String, Object> result3 = new HashMap<>();

        List<String> stringList = new ArrayList<>();
        String[] split = str.split(";");
        for (String s : split) {
            if (s.contains("private")) {
                String tmp = s.substring(s.indexOf("private") + "private".length() + 1);
                String substring = tmp.substring(tmp.indexOf(" ")).trim();
                stringList.add(substring);
            }
        }

        // 1. if and
        /**
         *
         * <if test="username != null and username != ''">
         *      and username = #{username}
         * </if>
         *
         */
        List<String> ifAndStringList = new ArrayList<>();
        for (String s : stringList) {
            String tmp = "<if test=\"" + s + " != null and " + s + " != ''\"> \n" +
                    "     and " + upperCaseToUnderline(s) + " = #{" + s + "}    \n" +
                    "</if>\n";
            ifAndStringList.add(tmp);
        }
        String join1 = String.join("", ifAndStringList);
        System.out.println(join1);

        // 2. and
        /**
         *
         * and username = #{username}
         *
         */
        List<String> andStringList = new ArrayList<>();
        for (String s : stringList) {
            String tmp = "and " + upperCaseToUnderline(s) + " = #{" + s + "}\n";
            andStringList.add(tmp);
        }
        String join2 = String.join("", andStringList);
        System.out.println(join2);

        // 3. json
        /**
         *
         * "username": "",
         *
         */
        List<String> jsonStringList = new ArrayList<>();
        String join3 = "";
        if (stringList.size() > 0) {
            jsonStringList.add("{\n");
            for (int i = 0; i < stringList.size(); i++) {
                String tmp = "";
                if (i == stringList.size() - 1) {
                    tmp = "\t\"" + stringList.get(i) + "\": \"\"\n";
                } else {
                    tmp = "\t\"" + stringList.get(i) + "\": \"\",\n";
                }
                jsonStringList.add(tmp);
            }
            jsonStringList.add("}\n");
            join3 = String.join("", jsonStringList);
            System.out.println(join3);
        }

        ret.put("ret1", join1);
        ret.put("ret2", join2);
        ret.put("ret3", join3);

        return ret;
    }

    private String upperCaseToUnderline(String str) {
        List<String> stringList = new ArrayList<>();
        for (int i = 0; i < str.length(); i++) {
            String lowerCase = String.valueOf(str.charAt(i)).toLowerCase();
            if (Character.isUpperCase(str.charAt(i))) {
                stringList.add("_");
                stringList.add(lowerCase);
            } else {
                stringList.add(lowerCase);
            }
        }
        return String.join("", stringList);
    }

}
